<script setup lang="ts">
import { requiredCheck, singleIpCheck } from "@/utils/verify";
import { ref, onMounted, nextTick } from "vue";
import { addData, editData } from "@/api/modules/system/account";
import { cfgList, cfgSetManageip, cfgPamGet, cfgPamSet } from "@/api/modules/system/params";
import { ElMessage } from "element-plus";
import SelectIfaceInput from "@/components/SelectIfaceInput/index.vue";

interface Props {
  title?: string;
  rowData?: Object;
}

const props = withDefaults(defineProps<Props>(), {
  title: "密码策略",
  rowData: undefined
});
defineOptions({
  name: "UpdateForm" // 默认策略
});
import useUpdate from "@/hooks/useUpdate";
import { ifaceAll, ifacemodeUpdate } from "@/api/modules/dynamic-camouflage/line-config";
import BaseButton from "@/components/BaseButton/index.vue";

const emits = defineEmits(["success"]);
const { updateFormData, updateFormRef, updateFormRules, submit, showHandle, openHandle, closeHandle } = useUpdate({
  rules: {
    min_length: [...requiredCheck("请选择长度")],
    complex: [...requiredCheck("请选择复杂度")],
    modtify_time: [...requiredCheck("请选择有效期")],
  },
  defaultData: {
    loginTime: 900
  }
});
const ifaces = ref([]);

async function getIface() {
  try {
    const res = await ifaceAll();
    console.log(res);
    if (res) {
      ifaces.value = Object.values(res);
      console.log(ifaces.value);
    }
    Object.values(res);
  } catch (error) {}
}

async function submitCb() {
  await submit({
    addApi: addData,
    updateApi: editData,
    sendData: {
      name: updateFormData.name,
      passwordRe: updateFormData.passwordRe || "",
      password: updateFormData.password || "",
      role: updateFormData.role,
      id: updateFormData?.id
    }
  });
  emits("success");
}

async function getCfg() {
  const res = await cfgPamGet();
  console.log("res", res);
  updateFormData.min_length = Number(res.min_length);
  updateFormData.modtify_time = Number(res.modtify_time);
  if (updateFormData.char_number == 1) {
  }
  if (res.char_number == 1 && res.upper_lower == 1 && res.special_char == 1) {
    updateFormData.complex = 3;
  } else if (res.char_number == 1 && res.upper_lower == 1) {
    updateFormData.complex = 2;
  } else if (res.char_number == 1) {
    updateFormData.complex = 1;
  }
}

async function setCfg() {
  await updateFormRef.value.validate();
  await cfgPamSet({
    pam: JSON.stringify({
      min_length: String(updateFormData.min_length),
      char_number: "1",
      upper_lower: [2, 3].includes(updateFormData.complex) ? "1" : "0",
      special_char: [3].includes(updateFormData.complex) ? "1" : "0",
      modtify_time: String(updateFormData.modtify_time),
      modtify_timestamp: 1760522414065
    })
  });
  ElMessage.success("更新成功");
  getCfg();
}

onMounted(() => {
  setTimeout(() => {
    getCfg();
  }, 1000);
});
const isEdit = ref(false);
</script>

<template>
  <el-card>
    <template #header>
      <div class="card-header h-box justify-between">
        <span>密码策略</span>
        <div class="card-footer h-box justify-end">
          <base-button @click="getCfg" type="refresh_icon_text"></base-button>
          <base-button :disabled="!isEdit" @click="setCfg" type="save_icon"></base-button>
          <base-button v-if="!isEdit" @click="isEdit = !isEdit" type="edit_text" class="mg-l-sm"></base-button>
          <base-button v-else @click="isEdit = !isEdit" type="cancel_icon" class="mg-l-sm"></base-button>
        </div>
      </div>
    </template>

    <el-form
      :disabled="!isEdit"
      ref="updateFormRef"
      :model="updateFormData"
      :rules="updateFormRules"
      label-width="80px"
      label-suffix="："
    >
      <el-form-item label="长度" prop="min_length">
        <el-select filterable v-model="updateFormData.min_length">
          <el-option v-for="item in 13" :label="item + 7 + '位'" :value="item + 7" :id="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="复杂度" prop="complex">
        <el-select v-model="updateFormData.complex">
          <el-option label="字母、数字" :value="1"></el-option>
          <el-option label="大小写字母、数字" :value="2"></el-option>
          <el-option label="大小写字母、数字、特殊字符" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="有效期" prop="modtify_time">
        <el-select v-model="updateFormData.modtify_time">
          <el-option label="无限" :value="0"></el-option>
          <el-option label="15天" :value="15"></el-option>
          <el-option label="30天" :value="30"></el-option>
          <el-option label="60天" :value="60"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<style lang="scss" scoped></style>
